<!-- <script src="<%= static_path(@conn, "/js/jquery-3.3.1.min.js") %>"></script> -->
<script src="<%= static_path(@conn, "/js/fetch.js") %>"></script>
<script>
function _get_stockname() {
    var stock_code = document.getElementById("ist_stock_code").value;
    console.log(stock_code);
	var token = document.getElementsByName('_csrf_token')[0]["value"];
	
	//var token = "<%= Plug.CSRFProtection.get_csrf_token() %>"
	//var token =  $('input[name="csrfToken"]').attr('value')
	//console.log(token);
	/*
   $.ajax({
		url: '/api/stock/get_stockname',
		method: 'post',
		headers: {
			'x-csrf-token': token
		},
		data: {
			code: stock_code
		},
		success: function (data) {
			$("#ist_stock_name").val(data);	
		},
		error : function(e) {
			console.info("Stock not found");
			$("#ist_stock_name").val("");	
		},
		done : function(e) {
			console.info("DONE");
		}
	});
	*/
	// fetch.js ile ajax muadili
	
	const request = async () => {
		const response = await fetch('/api/stock/get_stockname', {
		  method: 'POST',
		  headers: {
			'Content-Type': 'application/json',
			'x-csrf-token': token
		  },
		  credentials: 'same-origin',
		  body: JSON.stringify({code: stock_code})
		});
		if (response.status == "200"){
			const data = await response.json();
			document.getElementById("ist_stock_name").value=data;
		}else{
			console.log("stock not found");
			document.getElementById("ist_stock_name").value="";
		}
	}
	request();

	/*
	fetch('/api/stock/get_stockname', {
	  method: 'POST',
	  headers: {
		'Content-Type': 'application/json',
		'x-csrf-token': token
	  },
	  credentials: 'same-origin',
	  body: JSON.stringify({code: stock_code})
	}).then(function(response) {
		return response.json();
	}).then(function(data) {
		document.getElementById("ist_stock_name").value=data;
	}).catch(function(ex) {
		document.getElementById("ist_stock_name").value="";
	});
	*/
}

</script>
<h2>New Ist</h2>

<%= render "form.html", Map.put(assigns, :action, ist_path(@conn, :create)) %>

<span><%= link "Back", to: ist_path(@conn, :index) %></span>
